package cn.ondraw.ragflow.controller;


import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import cn.ondraw.common.core.domain.R;
import cn.ondraw.ragflow.pojo.ChatsAssistant;
import cn.ondraw.ragflow.service.IRagFlowAssistantService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 描述：聊天管理
 *
 * @author ageerle@163.com
 * @date 2023-03-01
 */
@Controller
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/ragflow/assistant")
public class RagFlowAssistantController {
    private final IRagFlowAssistantService assistantService;

    /**
     * 聊天记录
     */
    @GetMapping("/list/{modelName}")
    @ResponseBody
    public R<List<Map<String, String>>> list(@PathVariable String modelName) {
        System.out.println(modelName);
        List<ChatsAssistant> chatsAssistant = assistantService.getChatsAssistant(modelName);
        // 初始化结果列表，明确泛型类型
        List<Map<String, String>> chatsAssistantList = new ArrayList<>();

        // 检查 chatsAssistant 和其数据是否为空
        if (chatsAssistant != null && chatsAssistant.size() > 0) {
            chatsAssistant.forEach(item -> {
                // 确保 item 不为空，并且 id 和 name 都不为 null
                if (item != null && item.getId() != null && item.getName() != null) {
                    Map<String, String> map = Map.of("id", item.getId(), "name", item.getName());
                    chatsAssistantList.add(map);
                }
            });
        }
        return R.ok(chatsAssistantList);
    }

}
